package com.glorystone.director;


import com.rabbitmq.client.*;
import org.springframework.stereotype.Component;

//使用poll的方式消费
@Component
public class Consumer_Poll {

    public void receive() throws Exception{

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");// MQ的IP
        factory.setPort(5672);// MQ端口
        factory.setUsername("gavin");// MQ用户名
        factory.setPassword("iamgavin");// MQ密码
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();



        boolean autoAck = false;
        GetResponse response = channel.basicGet("hello", autoAck);
        if (response == null) {

        } else {
            AMQP.BasicProperties props = response.getProps();
            byte[] body = response.getBody();
            long deliveryTag = response.getEnvelope().getDeliveryTag();
            String message= new String(body,"UTF-8");
            channel.basicAck(deliveryTag, true); //
            System.out.println(message);
        }






    }





}
